package login.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.user.ProtoMessageLogin.AssetBundleType;
import game.module.user.ProtoMessageLogin.S2CAssetBundleList;
import lion.common.MsgCodeAnn;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.MySendToMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import login.LoginServer;
import login.LoginServerConfig;
import login.bean.AssetBundleBean;
import login.logic.AssetBundleManager;

@MsgCodeAnn(msgcode = 10045, accessLimit = 200)
public class AssetBundleListProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(AssetBundleListProcessor.class);

	@Override
	public void process(GamePlayer session, RequestMessageRaw request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void process(GamePlayer player, RequestProtoMessage request) throws Exception {
		logger.info("asset bundle list!ip={}", player.getAddress());
		// 服务器版本
		S2CAssetBundleList.Builder builder = S2CAssetBundleList.newBuilder().setTotalSize(59933013);
		// 下载地址
		LoginServerConfig loginServerConfig = LoginServer.getInstance().getLoginServerConfig();
		String assetBundleUrl = loginServerConfig.getAssetBundleUrl();
		builder.setBaseUrl(assetBundleUrl);
		// 内容
		List<AssetBundleBean> bundleList = AssetBundleManager.getInstance().getAssetBundleList();
		if (bundleList != null) {
			for (AssetBundleBean assetBundleBean : bundleList) {
				builder.addItemBuilder().setName(assetBundleBean.getName()).setMd5(assetBundleBean.getMd5())
						.setType(AssetBundleType.valueOf(assetBundleBean.getType()));
			}
		}
		// ret
		player.writeAndFlush(10046, builder);
	}

	@Override
	public void process(GamePlayer player, MyRequestMessage request) throws Exception {
		logger.info("10045-asset bundle list!ip={}", player.getAddress());
		// 服务器版本
		MySendToMessage retMsg = new MySendToMessage(player.alloc(), 10046);
		retMsg.writeInt(59933013);

		// S2CAssetBundleList.Builder builder =
		// S2CAssetBundleList.newBuilder().setTotalSize(59933013);
		// 下载地址
		LoginServerConfig loginServerConfig = LoginServer.getInstance().getLoginServerConfig();
		String assetBundleUrl = loginServerConfig.getAssetBundleUrl();
		// builder.setBaseUrl(assetBundleUrl);
		retMsg.writeString(assetBundleUrl);
		// 内容
		List<AssetBundleBean> bundleList = AssetBundleManager.getInstance().getAssetBundleList();
		retMsg.writeInt(bundleList.size());
		if (bundleList != null) {
			for (AssetBundleBean assetBundleBean : bundleList) {
				retMsg.writeString(assetBundleBean.getName()).writeString(assetBundleBean.getMd5());
				// builder.addItemBuilder().setName(assetBundleBean.getName()).setMd5(assetBundleBean.getMd5())
				// .setType(AssetBundleType.valueOf(assetBundleBean.getType()));
			}
		}
		// ret
		player.writeAndFlush(retMsg);
	}

}
